# from langchain.vectorstores import Pinecone
# import pinecone
import asyncio

from langchain.document_loaders.sitemap import SitemapLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores.chroma import Chroma
from langchain_openai import OpenAIEmbeddings

# %% Retrieve Answers
# db - Chroma
db = None
persist_directory = 'db'


# This function will help us in fetching the top relevent documents from our vector store - Pinecone
def from_existing_index(query, k=1):
    docs = None
    if db != None:
        retriever = db.as_retriever(search_kwargs={"k": k})  # k:2 -> 返回两个结果
        docs = retriever.get_relevant_documents(query)
    return docs


# Function to fetch data from website
# https://python.langchain.com/docs/modules/data_connection/document_loaders/integrations/sitemap
def get_website_data(sitemap_url):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    # SitemapLoader 加载网站的sitemap(包含网站信息)
    loader = SitemapLoader(
        sitemap_url
    )

    docs = loader.load()

    return docs


# Function to split data into smaller chunks
def split_data(docs):
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size=1000,
        chunk_overlap=200,
        length_function=len,
    )

    docs_chunks = text_splitter.split_documents(docs)
    return docs_chunks


# Function to create embeddings instance
def create_embeddings():
    embeddings = OpenAIEmbeddings()
    # embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
    return embeddings


# Function to push data to Pinecone
# def push_to_pinecone(pinecone_apikey, pinecone_environment, pinecone_index_name, embeddings, docs):
def push_to_pinecone(embeddings, docs):
    # pinecone.init(
    #     api_key=pinecone_apikey,
    #     environment=pinecone_environment
    # )

    # index_name = pinecone_index_name

    # index = Pinecone.from_documents(docs, embeddings, index_name=index_name)
    db = Chroma.from_documents(docs, embeddings, persist_directory=persist_directory)
    db.persist()  # 持久化
    return db


# Function to pull index data from Pinecone
# def pull_from_pinecone(pinecone_apikey, pinecone_environment, pinecone_index_name, embeddings):
def pull_from_pinecone(embeddings):
    # pinecone.init(
    #     api_key=pinecone_apikey,
    #     environment=pinecone_environment
    # )

    # index_name = pinecone_index_name

    # index = Pinecone.from_existing_index(index_name, embeddings)
    db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
    return db


# This function will help us in fetching the top relevent documents from our vector store - Pinecone Index
def get_similar_docs(index, query, k=2):
    # db - index
    similar_docs = index.similarity_search(query, k=k)
    return similar_docs
